home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / New System Software Extensions / QuickDraw™ GX 1.1.2 / Programming Stuff / Sample Code / Graphics Samples / Bouncing Bitmap ƒ / Bouncing Bitmap.c next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  5.3 KB  |  175 lines  |  [TEXT/KAHL]

  1. /*
  2.     Bouncing bitmap 
  3.     
  4.     This application will get a pixmap from its resource file.  It will scale it to a smaller size.
  5.     The pixmap (i.e. gBitmapShape) will then bounce around the window. We check to make sure the 
  6.     bitmapShape will be drawn within the bounds of the window by checking the location of the 
  7.     bitmapShape against the windowBoundsShape before each GXDrawShape call. The bitmapShape will be 
  8.     rotated 12 degrees before being drawn by GXDrawShape.  
  9.  
  10.     NOTES:
  11.     • This file requires the following files to run correctly:
  12.         "graphics shell.c", "graphics debug library.c", "qd library.c", "transform library.c".
  13.     
  14.     • For the best printing results, print this file in "landscape".
  15.  
  16.     ©1990 - 1994 Apple Computer, Inc. 
  17.     All rights reserved.
  18. */
  19.  
  20.  
  21. #include <Events.h>
  22. #include <Windows.h>
  23.  
  24. #include "font library.h"
  25. #include "graphics toolbox.h"
  26. #include "graphics libraries.h"
  27. #include "graphics debugging.h"
  28. #include "qd library.h"
  29. #include "graphics shell.h"
  30.  
  31. //
  32. //  Set up the title and size of the window 
  33. //
  34. Rect         gWindowQDRect = {40, 20, 340, 440};
  35. Str255         gWindowTitle = "\pBouncing bitmap";
  36.  
  37. //
  38. //    gGraphicsHeapSize sets the size of the graphics gxHeap created by calling the GXNewGraphicsClient routine
  39. //    in main () within graphics shell.c.  You can determine the amount of graphics gxHeap required by using GraphicsBug.
  40. //    With  gGraphicsHeapSize set to 60k, I had 5 free blocks left in the graphics gxHeap. Sounds good to me.
  41. //
  42. long        gGraphicsHeapSize = 80;
  43.  
  44. gxShape     gBitmapShape;
  45. gxRectangle    gBitmapShapeBounds;
  46. fixed         gDx, gDy, gDelta;
  47. gxRectangle gFixedWindowBounds;           //  bounding box of the window in local coordinates 
  48.  
  49.  
  50.  
  51. /*------ DoInitialization ---------------------------------------------------------------------------------*/
  52.  
  53. void DoInitialization(theWindow)
  54. WindowPtr theWindow;
  55. {
  56.     gxShape clipShape;
  57.  
  58.     //
  59.     //    Get the bounds of the window in GX coordinates.  We will use this information in the 
  60.     //    DoIdle function to make sure "Clarus" does not bounce outside of the window.
  61.     //
  62.     GXGetShapeBounds(gWindowBoundsShape, 0L, &gFixedWindowBounds);
  63.  
  64.     //
  65.     //    Get the pixmap from the a resource file. If we fail in our attempt to create gBitmapShape, 
  66.     //    GXValidateShape will let us know via the debugger. 
  67.     //
  68.     gBitmapShape = GetPixMapShape(128);
  69.     GXValidateShape (gBitmapShape);
  70.  
  71.       GXScaleShape(gBitmapShape, fixed1/2, fixed1/2, 0, 0);
  72.  
  73.     GXGetShapeBounds(gBitmapShape, 0L, &gBitmapShapeBounds);
  74.  
  75.     //
  76.     //  Setup the clip gxShape of the gBitmapShape
  77.     //
  78.     clipShape = GXNewRectangle(&gBitmapShapeBounds);
  79.     GXSetShapeClip(gBitmapShape, clipShape);
  80.     GXDisposeShape(clipShape);
  81.  
  82.     gDx = ff(5) + fixed1/2;
  83.     gDy = ff(6);
  84. }
  85.  
  86.  
  87.  
  88. /*------ DoIdle ----------------------------------------------------------------------------------------*/
  89.  
  90. void DoIdle(theWindow)
  91. WindowPtr theWindow;
  92. {
  93.     gxMapping         map;
  94.     gxShape         boundsShape;
  95.  
  96.     GXRotateShape(gBitmapShape, ff(12), (gBitmapShapeBounds.left + gBitmapShapeBounds.right >> 1), (gBitmapShapeBounds.top + gBitmapShapeBounds.bottom >> 1));
  97.  
  98.     //
  99.     //  Get the bounds of the gBitmapShape and setup a gxRectangle that contains the bounds.
  100.     //
  101.     GXGetShapeBounds(gBitmapShape, 0L, &gBitmapShapeBounds);
  102.     boundsShape = GXNewRectangle (&gBitmapShapeBounds);
  103.     
  104.     //
  105.     //    Get the gxMapping of the gBitmapShape and set boundsShape mapping to it. We use boundsShape
  106.     //    to setup the "new" "bounds" of our bouncing gBitmapShape. "bounds" is then used below to make
  107.     //    sure that "Clarus" does not bounce outside of the window.
  108.     //
  109.     GXGetShapeMapping(gBitmapShape, &map);
  110.     GXMapShape(boundsShape, &map);
  111.     GXGetShapeBounds(boundsShape, 0L, &gBitmapShapeBounds);
  112.  
  113.     //
  114.     //    Make sure "Marilyn" doe not bounce outside of the window, by adjusting the "new" location
  115.     //    appropriately.
  116.     //
  117.     if ((gDelta = gFixedWindowBounds.left - gBitmapShapeBounds.left) > 0 || (gDelta = gFixedWindowBounds.right - gBitmapShapeBounds.right) < 0)
  118.      {    
  119.         GXMoveShape(gBitmapShape, gDx + gDelta, ff(0));    
  120.         gDx = -gDx;    
  121.      }
  122.  
  123.  
  124.     if ((gDelta = gFixedWindowBounds.top - gBitmapShapeBounds.top) > 0 || (gDelta = gFixedWindowBounds.bottom - gBitmapShapeBounds.bottom) < 0)
  125.      {    
  126.          GXMoveShape(gBitmapShape, ff(0), gDy + gDelta);    
  127.          gDy = -gDy;
  128.      }
  129.  
  130.     GXMoveShape(gBitmapShape, gDx, gDy);
  131.     GXDrawShape(gBitmapShape);
  132.  
  133.     GXDisposeShape(boundsShape);
  134. }
  135.  
  136.  
  137. /*------ DoDraw ---------------------------------------------------------------------------------------*/
  138.  
  139. void DoDraw(theWindow)
  140. WindowPtr theWindow;
  141. {
  142.     GXDrawShape(gBitmapShape);
  143. }
  144.  
  145.  
  146.  
  147.  
  148. /*------ DoDispose ------------------------------------------------------------------------------------*/
  149.  
  150. void DoDispose(theWindow)
  151. WindowPtr theWindow;
  152. {
  153.     //  
  154.     //    You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good 
  155.     //    form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
  156.     //    call to DisposeWindow should dispose of the objects. If you are running the debugging version of the 
  157.     //    SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
  158.     //    can turn notices on in this file by setting gDebugging = TRUE (above).
  159.     //  
  160.     GXDisposeShape(gBitmapShape);
  161.     GXDisposeShape(gWindowBoundsShape);
  162.        DisposeWindow(theWindow);
  163. }
  164.  
  165.  
  166.  
  167. /*------ DoClick ---------------------------------------------------------------------------------------*/
  168.  
  169. void DoClick( orgMouseLoc, theWindow )
  170. gxPoint        orgMouseLoc;
  171. WindowPtr     theWindow;
  172. {
  173. }
  174.  
  175.